<?php
namespace loggin;
use think\facade\Request;
use Exception;

class Loggin{
    static private $action;
    static private $controller;

    static private function mkdir(){
        $module = Request::module(); //模块
        $controller = Request::controller(); //控制器
        $action = Request::action(); //方法
        self::$action = $action;
        self::$controller = $controller;
        $path = '';
        if(PATH_SEPARATOR==':'){
            if(!file_exists('/datalog')){
                mkdir('/datalog',0755,true);
            }
            $path = "/datalog/logs/$module";
        }else{
            if(!file_exists('D:/datalog')){
                mkdir('E:/datalog',0755,true);
            }
            $path = "E:/datalog/logs/$module";
        }
        //判断文件是否存在
        if(!file_exists($path)){
            mkdir($path,0755,true);
        }
        return $path;
    }

    static public function info($uid='',$str=''){
        try{
            $path = self::mkdir();
            if($path == '') throw new Exception("路径为空！！！");
            if($str == '') throw new Exception("内容为空！！！");
            $info = "[INFO] ".date("Y-m-d H:i:s")." [$uid][".self::$controller."][".self::$action."]".":".$str;
	        file_put_contents($path."/".date("Y-m-d").".txt",$info."\r\n", FILE_APPEND);
        }catch (\Exception $e) {
            exception($e->getMessage());
        }
    }

    static public function error($uid='',$str=''){
        try{
            $path = self::mkdir();
            if($path == '') throw new Exception("路径为空！！！");
            if($str == '') throw new Exception("内容为空！！！");
            $info = "[ERROR] ".date("Y-m-d H:i:s")." [$uid][".self::$controller."][".self::$action."]".":".$str;
	        file_put_contents($path."/".date("Y-m-d").".txt",$info."\r\n", FILE_APPEND);
        }catch (\Exception $e) {
            exception($e->getMessage());
        }
    }
}